<?php
class Model_Category extends MVC_MODEL {
/**
 * @var object SQL
 */
    public $oDb_category;
    private $rsListSiteMap ;
    private $test_id;

    public function __construct() {
        parent::__construct();
        $this->oDb_category = new Db_Categoiry();
        $this->rsListSiteMap=array();
    }
    public function insertCategory() {
        return $this->oDb_category->doInsert($this->getAllData(),false);
    }
    public function updateCategoryIcon($aData) {
        return $this->oDb_category->doUpdateCategory($aData);
    }
    public function check_exit_category_id($id) {
        return $this->oDb_category->check_exit_category_id($id);
    }
    public function updatePosition() {
        $this->oDb_category->updatePosition();
    // redir("?sMod=category_index");
    }
    public function updateCategory() {
        return $this->oDb_category->doUpdateCategory($this->getAllData());
    }

    public function getListCategory() {
        return $this->oDb_category->getListCategory($this);
    }

    public function getInfoCategory($id) {
        return $this->oDb_category->getRecordsetByPK($id);
    }

    public function getInfoCategoryByYahooID($YID) {
        return $this->oDb_category->getInfoCategoryByYahooID($YID);
    }
    public function deleteCategory($id) {
        return $this->oDb_category->doDeleteByPK($id);
    }

    /**
     *
     */
    public function getListCategoryParentChild() {
        $rsParent = $this->getListParentCategory();
        $this->setData('category_level',0);
        if(!$rsParent) {
            return false;
        }else {
            foreach ($rsParent as $rowParent) {
                $this->setData('category_level',1);
                if($rowParent['product_category_id'] == $this->getData('product_category_id')) {
                    continue;
                }
                $rowParent['category_level'] = $this->getData('category_level');
                $this->rsListSiteMap[] = $rowParent;
                $this->getListChildCategory($rowParent['product_category_id']);
            }
        }

        return $this->rsListSiteMap;
    }

    public function getListParentCategory() {
        return $this->oDb_category->getListParentCategory($this);
    }
    public function getListChildCategory($iParent_id) {
        $rsChild = $this->oDb_category->getListChildCategory($iParent_id);
        if(!$rsChild) {
            $this->setData('category_level', $this->getData('category_level')-1);
            return false;
        }else {
            $iNumChild = count($rsChild);
            $i=0;
            foreach ($rsChild as $rowChild) {
                $i++;
                $this->setData('category_level', $this->getData('category_level')+1);

                if($rowChild['product_category_parent_id'] == $this->getData('product_category_id') || $rowChild['product_category_id'] == $this->getData('product_category_id')) {
                    break;
                }
                $rowChild['category_level'] = $this->getData('category_level');
                $this->rsListSiteMap[] = $rowChild;

                $is_not_left_node = $this->getListChildCategory($rowChild['product_category_id']);
                if(!$is_not_left_node && $i == $iNumChild) {
                    $this->setData('category_level', $this->getData('category_level')-1);
                }
            }
        }
    }

    public function getListChildOnly($iParent_id) {
        return $this->oDb_category->getListChildCategory($iParent_id);
    }

    public function getLastestNumberOfProduct() {
        return $this->oDb_category->getLastestNumberOfProduct($this);
    }

    /**
     * PROCESS IMAGE
     */
    public function deleteImage($sImage) {
        $sPathMain = PATH_UPLOAD . "category/";
        $sPathThumb120 = $sPathMain . "thumb_20/";
        //die($sPathMain . $sImage);
        @unlink($sPathMain . $sImage);
        @unlink($sPathThumb120 . $sImage);
    }

    public function deleteThumbImage($sImage) {
        $sPathMain = PATH_UPLOAD . "category/";
        $sPathThumb120 = $sPathMain . "thumb_20/";
        //die($sPathMain . $sImage);
        //@unlink($sPathMain . $sImage);
        @unlink($sPathThumb120 . $sImage);
    }

    public function getFullImage($sImage) {
        $sPathMain = PATH_UPLOAD . "category/";
        $fileImage = $sPathMain . $sImage;

        if(is_file($fileImage)) {
            return URL_UPLOAD . "category/" . $sImage;
        }
        return URL_UPLOAD . "wrong.png";
    }

    public function getThumbImage($sImage, $iThumbWidth, $iThumbHeight) {
        $sPathMain = PATH_UPLOAD . "category/";
        $sPathThumb = "thumb_{$iThumbWidth}/";

        if(is_file($sPathMain . $sImage)) {
            if(!is_file($sPathMain . $sPathThumb . $sImage)) {
                $oThumb = new  thumbnail();
                $oThumb->setPathMain($sPathMain);
                $oThumb->setImage($sImage);

                $oThumb->setThumb($sPathThumb . $sImage);
                $oThumb->makeThumb($iThumbHeight, $iThumbWidth, 'resizewscale');
            }

            if(is_file($sPathMain . $sPathThumb . $sImage)) {
                return URL_UPLOAD . "category/" . $sPathThumb . $sImage;
            }else {
            /**
             * donnot create image thumbnail, because
             * image' width less than image thumbnail
             */
                return URL_UPLOAD . "category/" . $sImage;
            }
        }
        return URL_TEMPLATE_ADMIN . "image/tree_img/empty.gif";
    }
    /**
     * CHOOSE CATEGORY
     */
    public function getListParentCategoryChoose() {
        return $this->oDb_category->getListParentCategoryChoose($this);
    }
    public function getListChildCategoryChoose($parent_id) {
        return $this->oDb_category->getListChildCategoryChoose($parent_id);
    }
    public function doInsertGenCategoryId($gen_pro_id,$cat_id) {
        return $this->oDb_category->doInsertGenCategoryId($gen_pro_id,$cat_id);
    }

    //UPDATE ALL CHILD
    public function doUpdateAllChildStatus($parent_id) {
        $this->$test_id=null;
        $this->getAllChild($parent_id);
    }

    public function getAllChild($parent_id) {
        $rs=$this->oDb_category->getListChildCategory($parent_id);
        if(is_array($rs)) {
            foreach($rs as $row) {
                $cat_id=$row['product_category_id'];
                $parent_id=$row['product_category_parent_id'];
                if ($_POST['product_category_status'] == 'on')
                    $status= 1;
                else
                    $status=0;
                if($this->$test_id===$row['product_category_id']) {
                    break;
                }
                else {
                    $this->$test_id=$row['product_category_id'];
                    $this->oDb_category->doUpdateAllChildStatus($parent_id,$status);
                }

                $this->getAllChild($row['product_category_id']);
            }
        }
    }
}
